﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNet.Common.Kit;
using System.Windows.Forms;

namespace TB.Spider.Dianpu
{
    /// <summary>
    /// 关键词下找到的店铺信息
    /// </summary>
    public class KeySearchDianpu
    {
        string _keyword;  //传入关键词
        public string KeyWord
        {
            set { this._keyword = value; }
        }

        IniHelper ini = new IniHelper("HtmlNodeXPath.ini");
        LogerHelper logger = new LogerHelper(DirType.Local, MessageType.Success);

        List<KeySearchBb> ksBbList = new List<KeySearchBb>();  //所有页面的宝贝列表信息
        
        string _location="";  //分地区采集
        #region INI中读取的Node Path 值
        string _sellerXPath = ""; //输nick节点的XPath值。
        string _locationXPath = "";
        string _bbtitleXPath = "";   //与宝贝链接共用一个PATH
        string _picUrlXPath = "";
        string _bbxlXPath = "";
        string _priceXPath = "";
        //string _shopUrlXPath = "";
        #endregion

        System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser();
        public event TBErrorEventHandler NetError;  //网络错误事件
        public event TBErrorEventHandler IPShift;   //IP切换事件
        //public event UrlShiftEventHandler URLShift;  //URL变化事件

        public KeySearchDianpu()
        {
            _sellerXPath = ini.GetKeyValue("keySearch", "seller");
            _locationXPath = ini.GetKeyValue("keySearch", "location");
            _bbtitleXPath = ini.GetKeyValue("keySearch", "bbtitle");
            _picUrlXPath = ini.GetKeyValue("keySearch", "picUrl");
            _bbxlXPath = ini.GetKeyValue("keySearch", "bbxl"); ;
            _priceXPath = ini.GetKeyValue("keySearch", "price");
        }

        public string GetRequestUrl(string keyword,string location, int pageIndex)
        {
            string url = "";
            if (location == "")
            {
                url = "http://s.taobao.com/search?q=" + System.Web.HttpUtility.UrlEncode(keyword) +
                    "&s=" + pageIndex * 40 + "&style=grid";
            }
            else
            {
                url = url = "http://s.taobao.com/search?q=" + System.Web.HttpUtility.UrlEncode(keyword) +
                    "&s=" + pageIndex * 40 + "&style=grid" + "&loc=" + System.Web.HttpUtility.UrlEncode(location);
            }
            //url = "http://s.taobao.com/search?initiative_id=staobaoz_20131011&jc=1&style=grid&q=%B4%F2%B5%D7%C9%C0&stats_click=search_radio_all%3A1&promote=0&tab=all&bcoffset=-3&s=0#J_relative";
            return url;
        }

        public KeySearchDianpu(string key,string location)
        {
            this._keyword = key;
            this._location = location;
            //_shopUrlXPath = ini.GetKeyValue("keySearch", "shopUrl");
            _sellerXPath = ini.GetKeyValue("keySearch", "seller");
            _locationXPath = ini.GetKeyValue("keySearch", "location");
            _bbtitleXPath = ini.GetKeyValue("keySearch", "bbtitle");
            _picUrlXPath = ini.GetKeyValue("keySearch", "picUrl");
            _bbxlXPath = ini.GetKeyValue("keySearch", "bbxl"); ;
            _priceXPath = ini.GetKeyValue("keySearch", "price");
        }
        /// <summary>
        /// 以HttpWebRequest的方式访问关键词得到掌柜信息
        /// </summary>
        /// <returns></returns>
        public List<KeySearchDianpu> GetKeySearchDianpuByHttp()
        {
            List<KeySearchDianpu> list = new List<KeySearchDianpu>();
            HttpHelper httpHelper = new HttpHelper();
            HttpItem item = new HttpItem();
            item.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215;)";
            for (int i = 0; i <=1; i++)
            {
                item.URL = SetRequestUrl(i);
                string htmlStr = httpHelper.GetHtml(item).Html;  //while循环变量初始值
                HtmlDocHelper htmlHelper=new HtmlDocHelper(htmlStr);
                string htmlTitle = htmlHelper.GetHtmlTitle();
                while (!htmlTitle.Contains("淘宝搜索"))
                {

                }
                if (htmlStr.Contains("筛选条件加的太多啦"))
                {
                    break;  //退出FOR循环,寻找下一个
                }
                #region 页面返回参数 顺序
                //string _seller; //掌柜名  与 店铺共用一个XPATH
                //string _shopUrl;  //店铺URL

                //string _bbtitle;   //宝贝标题
                //string _bblink;  //宝贝链接

                //string _bbxl;   //宝贝销量
                //string _price; // 价格
                //string _picUrl; //图片地址
                //string _bbPos;  //宝贝位置
                #endregion 顺序
                //每页宝贝信息
                //GetDianpu(htmlStr);
            }
            return null;
        }

        /// <summary>
        /// 以浏览器控件的方式得到掌柜信息
        /// </summary>
        /// <returns></returns>
        public List<KeySearchDianpu> GetKeySearchDianpuByBrowser()
        {
            for (int i = 0; i <= 1; i++)
            {
                string url = url = "http://s.taobao.com/search?q="+_keyword+"&s=" + i * 40 + "&style=grid";
                //URLShift(url);  //相当于中断,将控制权交给调用者  
                //System.Threading.Thread.Sleep(10000);
            }
            return null;
        }
        static int ii = 1;
        public void GetDianpu(string htmlStr,int index)
        {
            HtmlDocHelper htmlHelper = new HtmlDocHelper(htmlStr);
            try
            {
                List<string[]> bbinfoPage = htmlHelper.GetNodes(_sellerXPath, _sellerXPath + ":href",    //店铺URL
                                                         _locationXPath, _bbtitleXPath, _bbtitleXPath + ":href",  //宝贝详情页面  
                                                         _bbxlXPath, _priceXPath, _picUrlXPath + ":src");  //图片URL
                int pos = 1;
                foreach (string[] str in bbinfoPage)
                {
                    LocLoggerHelper.WriteErrorLog(ii.ToString()+"-"+index.ToString()+"--",(index.ToString() + pos + "--" + str[0] + "  " + str[1] + " " + str[3] + "  " + str[4]));
                    pos++;
                }
            }
            catch (Exception ex)
            {
                LocLoggerHelper.WriteException("XX", ex);
            }
            ii++;
        }

        #region 类的私有函数部分
        private string SetRequestUrl(int pageIndex)
        {
            string url = "";
            if (_location == "")
            {
                url = "http://s.taobao.com/search?q=" + System.Web.HttpUtility.UrlEncode(_keyword) +
                    "&s=" + pageIndex * 40 + "&style=grid";
            }
            else
            {
                url = url = "http://s.taobao.com/search?q=" + System.Web.HttpUtility.UrlEncode(_keyword) +
                    "&s=" + pageIndex * 40 + "&style=grid" + "&loc=" + System.Web.HttpUtility.UrlEncode(_location);
            }
            //url = "http://s.taobao.com/search?initiative_id=staobaoz_20131011&jc=1&style=grid&q=%B4%F2%B5%D7%C9%C0&stats_click=search_radio_all%3A1&promote=0&tab=all&bcoffset=-3&s=0#J_relative";
            return url;
        }
        #endregion 类的私有函数部分 end
        public string htmlStr = "";
    }
}
